package com.googlecode.ivybeans.module.customizer;

import com.googlecode.ivybeans.facade.IvyLibrary;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

public class IvyLibraryUi {

    public static EditMediator registerEditMediatorProject(Project project, JCheckBox ivyEnabled, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt, File descriptorFile, File settingsFile) {
        EditMediator em = new EditMediator(project, ivyEnabled, ivyFileBtn, ivyFileTxt, settingsFileBtn, settingsFileTxt, descriptorFile, settingsFile);
        ivyFileBtn.addActionListener(em);
        settingsFileBtn.addActionListener(em);
        return em;
    }

    public final static class EditMediator implements ActionListener {

        private final Project project;
        private JCheckBox ivyEnabledCheck;
        private JButton ivyFileBtn;
        private JTextField ivyFileTxt;
        private JButton settingsFileBtn;
        private JTextField settingsFileTxt;
        private File selectedDescriptorFile;
        private File selectedSettingsFile;

        private EditMediator(Project project, JCheckBox ivyEnabledCheck, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt, File descriptorFile, File settingsFile) {
            this.project = project;
            this.ivyEnabledCheck = ivyEnabledCheck;
            this.ivyFileBtn = ivyFileBtn;
            this.ivyFileTxt = ivyFileTxt;
            this.settingsFileBtn = settingsFileBtn;
            this.settingsFileTxt = settingsFileTxt;
            this.selectedDescriptorFile = descriptorFile;
            this.selectedSettingsFile = settingsFile;
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(ivyEnabledCheck)) {
                
            }

            if (e.getSource().equals(ivyFileBtn)) {
                JFileChooser chooser = new JFileChooser();
                FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.setMultiSelectionEnabled(false);
                chooser.setDialogTitle(NbBundle.getMessage(IvyLibraryUi.class, "ivyFile.FileChooser.DialogTitle")); // NOI18N

                File curDir = FileUtil.toFile(project.getProjectDirectory());
                chooser.setCurrentDirectory(curDir);

                int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(ivyFileBtn)); // Sow the chooser

                if (option == JFileChooser.APPROVE_OPTION) {
                    selectedDescriptorFile = chooser.getSelectedFile();
                    ivyFileTxt.setText(selectedDescriptorFile.getAbsolutePath());
                }

            }
            if (e.getSource().equals(settingsFileBtn)) {
                JFileChooser chooser = new JFileChooser();
                FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.setMultiSelectionEnabled(false);
                chooser.setDialogTitle(NbBundle.getMessage(IvyLibraryUi.class, "settingsFile.FileChooser.DialogTitle")); // NOI18N

                File curDir = FileUtil.toFile(project.getProjectDirectory());
                chooser.setCurrentDirectory(curDir);

                int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(settingsFileBtn)); // Sow the chooser

                if (option == JFileChooser.APPROVE_OPTION) {
                    selectedSettingsFile = chooser.getSelectedFile();
                    settingsFileTxt.setText(selectedSettingsFile.getAbsolutePath());
        }

            }
        }

        public Project getProject() {
            return project;
        }

        public JCheckBox getIvyEnabledCheck() {
            return ivyEnabledCheck;
        }

        public JButton getIvyFileBtn() {
            return ivyFileBtn;
        }

        public JTextField getIvyFileTxt() {
            return ivyFileTxt;
        }

        public File getSelectedDescriptorFile() {
            return selectedDescriptorFile;
        }

        public File getSelectedSettingsFile() {
            return selectedSettingsFile;
    }

        public JButton getSettingsFileBtn() {
            return settingsFileBtn;
}

        public JTextField getSettingsFileTxt() {
            return settingsFileTxt;
        }
        
    }
}
